package com.jjb.www.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jjb.www.R;
import com.jjb.www.interfaces.IProgress;
import com.jjb.www.utils.DpUtil;


/**
 * Author: lkw
 * time:   2021/9/16 14:45
 * des:    条纹进度条
 */
public class StripeProgressBar extends FrameLayout implements IProgress {

    private RoundCornerImageView mProgressIv;
    private ImageView mBotIv;
    //private AppCompatTextView mProgressTextView;
    private int maxProgress;
    private int progress = 0;
    private int progressRadius;
    private boolean isVisible = true;

    public StripeProgressBar(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public StripeProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public StripeProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View root = LayoutInflater.from(context).inflate(R.layout.layout_stripe_progress_bar, this);
        //mProgressTextView = root.findViewById(R.id.tv_progress);
        mProgressIv = root.findViewById(R.id.p_cover_iv);
        mBotIv = root.findViewById(R.id.p_bot_iv);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StripeProgressBar);
        int defProgressRadius = DpUtil.dp2px(10);
        progressRadius = (int) ta.getDimension(R.styleable.StripeProgressBar_progress_radius, defProgressRadius);
        int progressBackgroundColor = ta.getColor(R.styleable.StripeProgressBar_progress_background, Color.parseColor("#d9d9d9"));
        Drawable progressImage = ta.getDrawable(R.styleable.StripeProgressBar_progress_image);
        maxProgress = ta.getInteger(R.styleable.StripeProgressBar_progress_max,100);
        if (progressImage == null) {
            throw new IllegalArgumentException("app:progressImage must not null.");
        }
        ta.recycle();
        mProgressIv.setImageDrawable(progressImage);
        mProgressIv.setRadiusPx(progressRadius);
        GradientDrawable drawable = new GradientDrawable();
        //设置圆角大小
        drawable.setCornerRadius(progressRadius);
        //设置shape背景色
        drawable.setColor(progressBackgroundColor);
        //设置到ImageView中
        mBotIv.setImageDrawable(drawable);
        setProgress(0,true);
    }

    /**
     * 设置进度条最大进度
     *
     */
    @Override
    public void setMax(int maxProgress) {
        this.maxProgress = maxProgress;
    }

    /**
     * 设置进度条当前进度
     *
     */
    @Override
    public void setProgress(int progress,boolean isVisible) {
        this.progress = progress;
        this.isVisible = isVisible;
        float percent = this.progress / (maxProgress * 1.0F);
//        if (isVisible){
//            mProgressTextView.setVisibility(VISIBLE);
//        }else {
//            mProgressTextView.setVisibility(GONE);
//        }
//        mProgressTextView.setText(String.format(Locale.CANADA,"%d%%", progress));
        mBotIv.post(() -> {
            final int ivWidth = mBotIv.getWidth();
            final int calcWidth = ivWidth - progressRadius * 2;
            LayoutParams lp = (LayoutParams) mProgressIv.getLayoutParams();
            int marginEnd = (int) ((1 - percent) * calcWidth);
            lp.width = ivWidth - marginEnd;
            mProgressIv.setLayoutParams(lp);
            mProgressIv.postInvalidate();

//            LinearLayout.LayoutParams lps = (LinearLayout.LayoutParams) mProgressTextView.getLayoutParams();
//            lps.width = ivWidth - marginEnd;
//            lps.leftMargin = lps.width;
//            mProgressTextView.setLayoutParams(lps);
//            mProgressTextView.postInvalidate();
        });
    }

    /**
     * 获取当前进度
     *
     */
    @Override
    public int getProgress() {
        return this.progress;
    }
}
